﻿using System;
using System.Text;
using OpenGameEngine.GameObjects;
using OpenGameEngine.Drawing;
using OpenGameEngine.Base;
using OpenGameEngine.Input;
using System.Windows.Forms;

namespace OpenGameEngine.Hud
{
    public class HudImage
        : GameObject
    {
        private Boolean _mouseEntered;
        private Boolean _mouseDown;

        public OpenGameEngine.Drawing.Bitmap Image { get; set; }

        public RectangleF ActualBounds { get; private set; }
        public SizeF ActualSize { get { return new SizeF(ActualBounds.Width, ActualBounds.Height); } }
        public Vector2 ActualPosition { get { return new Vector2(ActualBounds.X, ActualBounds.Y); } }
        public SizeF ProposedSize { get; set; }
        public Boolean AutoSize { get; set; }

        #region IDisposable
        private Boolean _disposed = false;

        ~HudImage()
        {
            if (!_disposed)
            {
                Dispose(false);
                _disposed = true;
            }
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                Dispose(true);
                _disposed = true;
            }
            GC.SuppressFinalize(this);
        }

        public void Dispose(Boolean disposeManagedResources)
        {
            if (disposeManagedResources)
            {

            }
        }
        #endregion

        public HudImage()
            : base()
        {
            this.Position = Vector2.Zero;
            this.Name = "HudImage.{" + this.UniqueID.ToString() + "}";
            this.Visible = true;
            this.Enabled = true;
            this.AcceptsMouseInput = true;
            this.AcceptsKeyboardInput = true;
            this.ProposedSize = new SizeF();
            this.AutoSize = true;
        }

        public HudImage(Bitmap bmp)
            : this()
        {
            Image = bmp;
        }

        public HudImage(Bitmap bmp, float x, float y)
            : this(bmp)
        {
            this.Position = new Vector2(x, y);
        }

        public HudImage(Bitmap bmp, float x, float y, float width, float height)
            : this(bmp, x, y)
        {
            this.AutoSize = false;
            this.ProposedSize = new SizeF(width, height);
        }

        public HudImage(Bitmap bmp, Vector2 location, SizeF size)
            : this(bmp, location.X, location.Y, size.Width, size.Height)
        {

        }

        private void UpdateMetrics(GameCoreEventArgs e)
        {
            if (Image != null)
            {
                SizeF _size;
                if (AutoSize)
                {
                    _size = Image.Size;
                }
                else
                {
                    _size = this.ProposedSize;
                }

                this.ActualBounds = new RectangleF(this.Position, _size);
            }
            else
                this.ActualBounds = new RectangleF(this.Position, this.ProposedSize);

        }

        private void CheckForInput(GameCoreEventArgs e)
        {
            if (AcceptsMouseInput)
            {
                MouseStateInfo prevMouse = e.Core.PreviousMouseStateInfo;
                MouseStateInfo mouse = e.Core.CurrentMouseStateInfo;
                Vector2 v = mouse.MouseLocation;
                RectangleF d = this.ActualBounds;

                if (v.X > d.X && v.Y > d.Y && v.X < (d.X + d.Width) && v.Y < (d.Y + d.Height))
                {
                    if (!_mouseEntered)
                    {
                        MouseEnter(e);
                        _mouseEntered = true;
                    }

                    foreach (MouseButtons b in Globals.MouseButtons)
                    {
                        bool blnCurrMs = mouse[b];
                        bool blnPrevMs = prevMouse[b];

                        if (blnCurrMs)
                        {
                            MouseDown(e, b);
                        }
                        else if (blnPrevMs && !blnCurrMs)
                        {
                            MouseUp(e, b);
                            MouseClick(e, b);
                        }
                    }
                }
                else
                {
                    if (_mouseEntered)
                    {
                        MouseLeave(e);
                        _mouseEntered = false;
                    }
                }
            }
        }

        public override void Update(Base.GameCoreEventArgs e)
        {
            base.Update(e);

            UpdateMetrics(e);
            CheckForInput(e);
        }

        private void DrawImage(DrawEventArgs e)
        {
            e.DrawImage(this.Image, this.ActualBounds);
        }

        public override void Draw(DrawEventArgs e)
        {
            base.Draw(e);

            DrawImage(e);
        }
    }
}
